import {Msg} from 'core';
/**
 * 微信权限判断
 * @param scope 地理位置：'scope.userLocation'
 * @returns {Promise<unknown>}
 */
function authorize(scope, scopeName=''){
    return new Promise((resolve, reject) => {
        uni.authorize({
            scope: scope,
            success: ()=> {
                resolve(true)
            },
            fail: (res)=> {
                console.log(res);
                resolve(false);
                if(res.errMsg == "authorize:fail auth deny") {
                    Msg.confirm('检测到' + scopeName + '权限被拒绝，是否前往设置中心开启权限').then(_ => {
                        uni.openSetting()
                    })
                } else if(res.errno == 112) {
                    Msg.error("隐私政策未设置，请联系客服");
                } else {
                    Msg.error("未授权，请联系客服");
                }
            }
        })
    })
}

export default authorize;

export function authorizeUserLocation() {
    return authorize('scope.userLocation', '获取当前地理位置')
}

export function authorizeGetUserInfo() {
    return authorize('scope.userInfo', '获取用户信息')
}
